<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>用户登录和注册</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      background-color: #f4f4f4;
    }

    .container {
      display: flex;
      justify-content: space-around;
      align-items: center;
      width: 80%;
      max-width: 800px;
    }

    .form-box {
      background-color: #fff;
      padding: 20px;
      border-radius: 8px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
      width: 45%;
    }

    h1 {
      text-align: center;
      color: #333;
      margin-bottom: 20px;
    }

    label {
      display: block;
      margin-bottom: 5px;
      color: #555;
    }

    input[type="text"],
    input[type="password"],
    button {
      width: 100%;
      padding: 8px;
      margin-bottom: 10px;
      border-radius: 4px;
      border: 1px solid #ccc;
      box-sizing: border-box;
    }

    button {
      background-color: #007bff;
      color: #fff;
      cursor: pointer;
    }

    button:hover {
      background-color: #0056b3;
    }

    hr {
      margin: 20px 0;
    }
    a {
      color: #007bff;
      text-decoration: none;
      transition: color 0.3s ease-in-out;
    }

    a:hover {
      color: #0056b3;
      text-decoration: underline;
    }
  </style>
</head>
<body>

<div class="container">
  <div id="loginForm" class="form-box">
    <h1>统一身份用户登录</h1>
    <div>
      <label for="loginName">用户名:</label>
      <input type="text" id="loginName">
    </div>
    <div>
      <label for="loginPassword">密码:</label>
      <input type="password" id="loginPassword">
    </div>
    <button onclick="login()">登录</button>
    <hr>
    <p>没有账号？<a href="#" onclick="showRegisterForm()">点击注册</a></p>
  </div>

  <div id="registerForm" class="form-box" style="display: none;">
    <h1>用户注册</h1>
    <div>
      <label for="registerName">用户名:</label>
      <input type="text" id="registerName">
    </div>
    <div>
      <label for="registerPassword">密码:</label>
      <input type="password" id="registerPassword">
    </div>
    <button onclick="register()">注册</button>
    <hr>
    <p>已有账号？<a href="#" onclick="showLoginForm()">点击登录</a></p>
  </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
  const BASE_URL = 'http://localhost:8082'; // 替换为您的服务器URL

  function register() {
    const name = $('#registerName').val();
    const password = $('#registerPassword').val();t

    $.ajax({
      url: `${BASE_URL}/user/register`,
      method: 'POST',
      contentType: 'application/json',
      data: JSON.stringify({ Name: name, Password: password }), // 确保属性名称与后端匹配
      success: function(response) {
        alert('注册成功！'); // 确保访问Reply字段
      },
      error: function(error) {
        alert('注册失败，请重试。');
      }
    });
  }

  function login() {
    const name = $('#loginName').val();
    const password = $('#loginPassword').val();

    $.ajax({
      url: `${BASE_URL}/user/login`,
      method: 'POST',
      contentType: 'application/json',
      data: JSON.stringify({ Name: name, Password: password }), // 确保属性名称与后端匹配
      success: function(response) {
        alert('登录成功！'); // 确保访问Reply字段
      },
      error: function(error) {
        alert('登录失败，请检查您的凭据。');
      }
    });
  }

  function showRegisterForm() {
    document.getElementById('loginForm').style.display = 'none';
    document.getElementById('registerForm').style.display = 'block';
  }

  function showLoginForm() {
    document.getElementById('registerForm').style.display = 'none';
    document.getElementById('loginForm').style.display = 'block';
  }
</script>

</body>
</html>
